package commons;


import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author mbudzilo
 */
@SessionScoped
@ManagedBean(name="AuthenticationBean")
public class AuthenticationBean implements Serializable{
  public static final String AUTH_KEY = "app.user.name";
  public static final String AUTH_TYPE = "none";
  private String template = "./layout.xhtml";
  private String name,pass;
  SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");    
  Date data = new Date();
  String time=sdf.format(data);
  public String getTime() {
        return time;
  }
  public void setTime(String time) {
        this.time = time;
  }  
  public String getTemplate() {
        return template;
  }
  public void setTemplate(String template) {
        this.template = template;
  }  
  public String getPass() {
        return pass;
  }
  public void setPass(String pass) {
        this.pass = pass;
  }
  public String getName() { 
      return name; 
  }
  public void setName(String name) {
      this.name = name;
  }
  public boolean isLoggedIn() {
    return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(AUTH_KEY) != null;
  }
  public String login() {
       try {
            Connection conn = (Connection) DbConnection.getConnection();
            String query ="select l.log_haslo ,"
                    + " r.rol_nazwa "
                    + "from dane_logowania l"
                    + "join rola r ON l.log_rol_id = r.rol_id "
                    + "where l.log_nazwa=?" ;
            PreparedStatement prepStmt = conn.prepareStatement(query);
            prepStmt.setString(1, this.name);
            ResultSet rs = prepStmt.executeQuery();
            
            if(rs.next()){
                String haslo = rs.getString("log_haslo");
                String typ = rs.getString("rol_nazwa");
                
                if(haslo.equals(this.pass)){
                    // zalogowany
                    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(AUTH_KEY, name);
                    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(AUTH_TYPE, typ);
                    if(typ.equals("klient")){
                        setTemplate("/uzytkownik/layout_1.xhtml");
                      
                        return "uzytkownik";                        
                    }else if(typ.equals("koordynator")){
                        setTemplate("/magazyn/layout_2.xhtml");
                        return "magazyn";
                    }else if(typ.equals("operator")){
                        setTemplate("/wypozyczalnia/layout_3.xhtml");
                        return "wypozyczalnia";
                    }else{
                        return "fail";
                    }                    
                }
                
            }else{
                // nie znaleziono takiego loginu                
                return "fail";
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(AuthenticationBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "fail";	
  }
  public String logout() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(AUTH_KEY);
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(AUTH_TYPE);
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("mojeKsiazkiManager");
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("WypozyczKsiazkeManager");
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("edytujUzytkownika");
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("dodawanieKsiazkiManager");
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("wyszukajWplate");
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("ujemneSaldo");
    FacesContext.getCurrentInstance().getExternalContext().redirect("/ssi/");

    setTemplate("./layout.xhtml");
    return "logout";
  }
}


